<?php

session_start();

require_once './db/arena-park-database.php';

require_once './doc/generate_doc.php';

require_once './email/MailMannagament.php';

if (isset($_REQUEST['type'])) {

	$dataset = new ArenaParkDatabase();
	$fileM = new FileMannagament();

	if ($_REQUEST['type'] == 'insert') {

		$inserts = array();
		$generateDocParam = array();

		if (isset($_REQUEST['user'])) {

			if (isset($_SESSION['enterprise']))
				$_REQUEST['user'][] = array('member', $_SESSION['enterprise']);

			if (!$dataset->insert_array('user', $_REQUEST['user'])) {
				echo '
{  
  "fail": 1	  
}';
				exit;
			}
			$user = $dataset->get_record_by_ID('user', 'userid', $dataset->get_last_id("user"));

			$_SESSION['userid'] = $user['userid'];
			$_SESSION['name'] = $user['name'];
			$_SESSION['last_name'] = $user['last_name'];
			$_SESSION['sex'] = $user['sex'];

			$conductor['userid'] = $user['userid'];
			$conductor['name'] = $user['name'];
			$conductor['last_name'] = $user['last_name'];
			$conductor['sex'] = $user['sex'];
			$conductor['address'] = $user['address'];
			$conductor['postal_code'] = $user['postal_code'];
			$conductor['phone_movil'] = $user['phone_movil'];

			$dataset->insert_array('conductor', $conductor, false);

			$conductor = $dataset->get_record_by_ID('conductor', 'conductorid', $dataset->get_last_id("conductor"));

			if (isset($_SESSION['enterprise'])) {

				$conductorE = array(
					'conductorid' => $conductor['conductorid'],
					'enterprisesid' => $_SESSION['enterprise']
				);
				$dataset->insert_array('conductor_enterprises', $conductorE, false);

				echo '
{  
  "name": "' . $user['name'] . '",
  "last_name": "' . $user['last_name'] . '",
  "userid": "' . $user['userid'] . '",
  "sex": "' . $user['sex'] . '",
  "email": "' . $user['email'] . '",
  "conductorid": "' . $conductor['conductorid'] . '",
  "namec": "' . $conductor['name'] . '",
  "last_namec": "' . $conductor['last_name'] . '",
  "sexc": "' . $conductor['sex'] . '",
  "phone_movilc": "' . $conductor['phone_movil'] . '",
  "postal_codec": "' . $conductor['postal_code'] . '",
  "addressc": "' . $conductor['address'] . '"			  
}';
				
			} else {
				echo '
{  
  "name": "' . $user['name'] . '",
  "last_name": "' . $user['last_name'] . '",
  "userid": "' . $user['userid'] . '",
  "conductorid": "' . $conductor['conductorid'] . '",
  "sex": "' . $user['sex'] . '",
  "email": "' . $user['email'] . '"
}';
				
			}
		} else if (isset($_REQUEST['reservation'])) {

			$car = $_REQUEST['car'];
			$conductor = $_REQUEST['conductor'];
			if (isset($_SESSION['userid']))
				$user = $dataset->get_record_by_ID('user', 'userid', $_SESSION['userid']);
			else {
				$user = $dataset->get_record_by_ID('user', 'userid', $_REQUEST['userid']);
			}
//echo var_dump( $_REQUEST['reservation']);exit;
			if (!($reservation = $dataset->setReservation($user, $_REQUEST['reservation'], $car, $conductor))) {
				echo 0;
				exit;
			}
			$sqlService = "SELECT service.charge, service.name
FROM service
INNER JOIN reservation_service
WHERE service.serviceid = reservation_service.serviceid AND reservation_service.reservation_number = " . $reservation['reservation_number'] . "";

			$services = $dataset->exec_query($sqlService);

			$contract = $fileM->generateContract($user, $car, $reservation, $conductor, $services);

			$mail = new MailMannagament();

			echo $mail->sendContract($user, $contract);

			exit;
		} else if (isset($_REQUEST['conductor'])) {

			if (isset($_REQUEST['enterprise'])) {
				$result = $dataset->setConductor($_REQUEST['conductor']);
				$conductor = $dataset->get_record_by_ID('conductor', 'conductorid', $dataset->get_last_id("conductor"));
//				echo var_dump($_SESSION['enterprise']);exit;
				$dataset->insert_array('conductor_enterprises', array("conductorid" => $conductor['conductorid'], "enterprisesid" => $_SESSION['enterprise']), false);
			} else {
				$result = $dataset->setConductor($_REQUEST['conductor'], $_SESSION['userid']);
				$conductor = $dataset->get_record_by_ID('conductor', 'conductorid', $dataset->get_last_id("conductor"));
			}
			if ($result) {

				echo '
{  
  "name": "' . $conductor['name'] . '",
  "last_name": "' . $conductor['last_name'] . '",
  "conductorid": "' . $conductor['conductorid'] . '"	  
}';
			} else {
				echo '
{  
  "fail": 1	  
}';
			}
		} else if (isset($_REQUEST['car'])) {

			if (isset($_REQUEST['enterprise'])) {

				$result = $dataset->setCar($_REQUEST['car']);
				$car = $dataset->get_record_by_ID('car', 'idcar', $dataset->get_last_id("car"));
				$dataset->insert_array('car_enterprises', array("carid" => $car['idcar'], "enterprisesid" => $_SESSION['enterprise']), false);
			} else {
				$result = $dataset->setCar($_REQUEST['car'], $_SESSION['userid']);
				$car = $dataset->get_record_by_ID('car', 'idcar', $dataset->get_last_id("car"));
			}
			if ($result) {

				echo '
{  
  "mark": "' . $car['mark'] . '",
  "carid": "' . $car['idcar'] . '"	  
}';
			} else {
				echo '
{  
  "fail": 1	  
}';
			}
		}
	} else if ($_REQUEST['type'] == 'get') {

		if (isset($_REQUEST['car'])) {

			$car = $dataset->get_record_by_ID('car', 'idcar', $_REQUEST['car']);

			echo '
{  
  "mark": "' . $car['mark'] . '",
  "model": "' . $car['model'] . '",
  "plate": "' . $car['plate'] . '",	  
  "color": "' . $car['color'] . '"
}';
		} else if (isset($_REQUEST['conductor'])) {

			$conductor = $dataset->get_record_by_ID('conductor', 'conductorid', $_REQUEST['conductor']);

			echo '
{  
  "name": "' . $conductor['name'] . '",
  "last_name": "' . $conductor['last_name'] . '",
  "address": "' . $conductor['address'] . '",
  "phone_movil": "' . $conductor['phone_movil'] . '",
  "sex": "' . $conductor['sex'] . '",
  "postal_code": "' . $conductor['postal_code'] . '"
}';
		}
	} else if ($_REQUEST['type'] == 'loguin') {

		if (isset($_REQUEST['credentials'])) {

			if (!($user = $dataset->loguin($_REQUEST['credentials']))) {
				echo 0;
				exit;
			}


			echo '
{  
  "name": "' . $user[0]['name'] . '",
  "last_name": "' . $user[0]['last_name'] . '",
  "userid": "' . $user[0]['userid'] . '"	  
}';
			$_SESSION['userid'] = $user[0]['userid'];
			$_SESSION['name'] = $user[0]['name'];
			$_SESSION['last_name'] = $user[0]['last_name'];
			$_SESSION['sex'] = $user[0]['sex'];
		}
	} else if ($_REQUEST['type'] == 'loguin-enterprise') {

		if (isset($_REQUEST['credentials'])) {

			if (!($enterprise = $dataset->loguinEnterprise($_REQUEST['credentials']))) {
				echo 0;
				exit;
			}

			$userfromenterprise = $dataset->getUserFromEnterprise($enterprise[0]['enterpriseid']);
			echo '
[
{  
  "name": "' . $enterprise[0]['name'] . '"	  
},
{  
  "AAAAAAA": "' . $enterprise[0]['name'] . '"	  
}
]';
			$_SESSION['enterprise'] = $enterprise[0]['enterpriseid'];
			$_SESSION['enterprise_name'] = $enterprise[0]['name'];
		}
	} else if ($_REQUEST['type'] == 'getUserById') {

		if (isset($_REQUEST['user'])) {

			if (!($user = $dataset->getUserById($_REQUEST['user']))) {
				echo 0;
				exit;
			}

			echo '
{  
  "name": "' . $user[0]['name'] . '",
  "last_name": "' . $user[0]['last_name'] . '",
  "userid": "' . $user[0]['userid'] . '",
  "address": "' . $user[0]['address'] . '",
  "postal_code": "' . $user[0]['postal_code'] . '",
  "country": "' . $user[0]['country'] . '",
  "phone_movil": "' . $user[0]['phone_movil'] . '",
  "phone_home": "' . $user[0]['phone_home'] . '",
  "email": "' . $user[0]['email'] . '",
  "fax": "' . $user[0]['fax'] . '",
  "member": "' . $user[0]['member'] . '",
  "sex": "' . $user[0]['sex'] . '"
}';
		}
	} else if ($_REQUEST['type'] == 'updateUser') {

		if (isset($_REQUEST['user'])) {

			if (!($userid = $dataset->updateUser($_REQUEST['user']))) {
				echo 0;
				exit;
			}
			$user = $dataset->getUserById($userid);
			echo '
{  
  "name": "' . $user[0]['name'] . '",
  "last_name": "' . $user[0]['last_name'] . '",
  "userid": "' . $user[0]['userid'] . '"	  
}';
		}
	} else if ($_REQUEST['type'] == 'updateConductor') {

		if (isset($_REQUEST['conductor'])) {

			if (!($conductorid = $dataset->updateConductor($_REQUEST['conductor']))) {
				echo 0;
				exit;
			}
			$conductor = $dataset->get_record_by_ID('conductor', 'conductorid', $conductorid);
			echo '
{  
  "name": "' . $conductor['name'] . '",
  "last_name": "' . $conductor['last_name'] . '",
  "address": "' . $conductor['address'] . '",
  "phone_movil": "' . $conductor['phone_movil'] . '",
  "sex": "' . $conductor['sex'] . '",
  "postal_code": "' . $conductor['postal_code'] . '",
  "conductorid": "' . $conductorid . '"
}';
		}
	} else if ($_REQUEST['type'] == 'updateCar') {

		if (isset($_REQUEST['car'])) {

			if (!($carid = $dataset->updateCar($_REQUEST['car']))) {
				echo 0;
				exit;
			}
			$car = $dataset->get_record_by_ID('car', 'idcar', $carid);
			echo '
{  
  "mark": "' . $car['mark'] . '",
  "model": "' . $car['model'] . '",
  "plate": "' . $car['plate'] . '",	  
  "color": "' . $car['color'] . '",
  "carid": "' . $car['idcar'] . '"
}';
		}
	} else if ($_REQUEST['type'] == 'logout') {

		if (isset($_SESSION['userid'])) {
			unset($_SESSION['userid']);
			unset($_SESSION['name']);
			unset($_SESSION['last_name']);
		} else if (isset($_SESSION['enterprise'])) {
			unset($_SESSION['enterprise']);
		}
		echo 1; return;
	} else if ($_REQUEST['type'] == 'contact') {

		$mail = new MailMannagament();

		echo $mail->sendContactRequest(ASimpleMySQLDB::normalizeQuery($_REQUEST['data']));

		exit;
	}
}
exit;
?>
